/**
 * JsMenuFragmentx
 * 继承:
 * androidx.fragment.app.Fragment;
 */

package com.vgemv.jsutilitysdk.Menu.JsMenuFragment.JsMenuFragmentx;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewRefreshCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewTouchThroughCallback;

public class JsMenuFragmentx extends Fragment  {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    public JsMenuViewBasicCallback mBasicCallback;
    public JsMenuViewRefreshCallback mRefreshCallback;
    public JsMenuViewTouchThroughCallback mTouchThroughCallback;


    public JsMenuView mMenuView;

    private int setupUiCount = 0;

    public JsMenuFragmentx() {
        super();
        //此时没有Context,无法创建view.不能在这里setupUi.
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

//    public ArrayList<JsMenuItemVm> vms() {
//        return mal_vms;
//    }

    //需要返回一个布局,作为Fragment的根视图.
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMenuView = new JsMenuView(getContext());
        mMenuView.mBasicCallback = mBasicCallback;
        mMenuView.mRefreshCallback = mRefreshCallback;
        return mMenuView;
    }



    @Override
    public void onResume() {
        super.onResume();
    }


    // 2 InnnerFunction


    // 3 Interface

    /**
     * 将当前fragment的rootView添加到指定的ContainerView中.
     * 这里设置会是占满ContainerView.
     * @param activity  指定的Activity
     * @param containerView      目标ContainerView
     */
    public void moveToParentView(FragmentActivity activity, View containerView){
        //containerView必须有id,如果没有设置.
        int id = containerView.getId();
        if(id < 0) {
            containerView.setId(View.generateViewId());
        }
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(containerView.getId(),this);
        fragmentTransaction.show(this);
        fragmentTransaction.commit();
    }




    // 1 更新数据
    /**
     * 1.1 全部数据刷新
     * 如果mAdapter还没初始化就要求reload,其实可以不reload.
     */
    public void reloadData() {
      mMenuView.reloadData();
    }

    /**
     * 1.2 增加数据刷新
     * @param insertedPosition      数据增加的位置.
     */
    public void  loadInsertedItem(int insertedPosition) {
        mMenuView.loadInsertedItem(insertedPosition);
    }



    // 2 上拉加载和下拉刷新
    //
    /**
     * 结束刷新
     isSuccess
     YES:
     dataCount > 0
     * 已加载页数为1
     * 结束刷新
     * 结束下拉加载且可以加载.

     dataCount = 0
     * 已加载页数为0
     * 结束刷新
     * 结束下拉加载且不可以加载.
     * 如果有EmptyView应该显示EmptyView

     NO:仅仅结束刷新,其余状态不改变
     */
    public void finishRefresh(boolean isSuccess,boolean hasData) {
        mMenuView.finishRefresh(isSuccess,hasData);
    }

    /**
     * 结束加载
     isSuccess:此次加载是否成功,如果成功
     YES:将根据dataCount设置是否还有更多的数据. 0表示没有更多数据,已加载页数不变;非0,表示还有数据,已加载页+1;
     NO:仅仅结束加载,其余状态不发生改变.
     */
    public void finishLoadMore(boolean isSuccess,boolean hasData) {
       mMenuView.finishLoadMore(isSuccess,hasData);
    }

    public void refresh() {
        mMenuView.refresh();
    }


    // 3 位置控制

    /**
     * 3.1 滚动到顶部
     *
     */
    public void scrollToTop() {
        //RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
//        mRecyclerView.scrollTo(0,0);
        mMenuView.scrollToTop();
    }

    public void scrollToBottom() {
        mMenuView.scrollToBottom();
    }


    /**
     * 4 选择指定位置的item
     */
    public void chooseItem(int index) {
       mMenuView.chooseItem(index);
    }

    public Integer selectedIndex() {
        return mMenuView.selectedIndex();
    }

    public int loadedPage() {
        return mMenuView.loadedPage();
    }

    public JsMenuViewAdapter getAdapter() {
        return mMenuView.getAdapter();
    }
}